<template>
  <div class="app-container">
  
    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <div class="data-table__toolbar--actions">
          <el-button
            v-hasPerm="['group:level:add']"
            type="success"
            icon="plus"
            @click="handleOpenDialog()"
          >
            新增
          </el-button>
          <el-button
            v-hasPerm="['group:level:delete']"
            type="danger"
            :disabled="selectIds.length === 0"
            icon="delete"
            @click="handleDelete()"
          >
            删除
          </el-button>
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="Lists"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        class="data-table__content"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="序号"  />
        <el-table-column prop="name" label="团队级别" />
        <el-table-column prop="rate" label="收益比例"  />
        <el-table-column prop="sort" label="排序"  />
        <el-table-column prop="createTime" label="创建时间"  />

        <el-table-column label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            
            <el-button
              v-hasPerm="['group:level:edit']"
              type="primary"
              link
              size="small"
              icon="edit"
              @click.stop="handleOpenDialog(scope.row.id,scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-hasPerm="['group:level:delete']"
              type="danger"
              link
              size="small"
              icon="delete"
              @click.stop="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="600px"
      @closed="handleCloseDialog"
    >
      <el-form ref="FormRef" :model="formData" :rules="rules" label-width="120px">
        
        <el-form-item label="团队级别" prop="name">
          <el-input v-model="formData.name" placeholder="请输入团队级别" />
        </el-form-item>

        <el-form-item label="收益比例" prop="rate">
          <el-input v-model="formData.rate" placeholder="请输入收益比例" />
        </el-form-item>
        
        <el-form-item label="显示排序" prop="sort">
          <el-input-number
            v-model="formData.sort"
            controls-position="right"
            style="width: 100px"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="状态" style="display: none;">
          <el-radio-group v-model="formData.status">
            <el-radio :value="1">正常</el-radio>
            <el-radio :value="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "团队等级",
  inheritAttrs: false,
});

import GroupAPI, { LevelVO, LevelForm } from "@/api/system/group.api";

// const queryFormRef = ref();
const FormRef = ref();

const loading = ref(false);
const selectIds = ref<number[]>([]);
const queryParams = reactive<any>({});

const dialog = reactive({
  title: "",
  visible: false,
});

const Lists = ref<LevelVO[]>();
const formData = reactive<LevelForm>({
  /** ID(新增不填) */
  id: '0',
  /** 团队级别名称 */
  name: '',
  /** 收益比例 */
  rate: '',
  /** 排序 */
  sort: 1,
  /** 状态(1:启用；0：禁用) */
  status: 1
});

const rules = reactive({
  name: [{ required: true, message: "团队级别不能为空", trigger: "blur" }],
  sort: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
});



// 查询
function handleQuery() {
  console.log(queryParams)
  loading.value = true;

  Lists.value =  [
    {
      id:"1",
      name: "一级团队",
      sort:1,
      createTime:"2023-10-10 10:00:00",
      rate:"50%",
    },
    {
      id:"2",
      name: "二级团队",
      sort:2,
      createTime:"2023-10-10 10:00:00",
      rate:"30%",
    },
    {
      id:"3",
      name: "三级团队",
      sort:3,
      createTime:"2023-10-10 10:00:00",
      rate:"20%",
    }
  ]
  loading.value = false;
}

// 重置查询
// function handleResetQuery() {
//   queryFormRef.value.resetFields();
//   handleQuery();
// }

// 处理选中项变化
function handleSelectionChange(selection: any) {
  selectIds.value = selection.map((item: any) => item.id);
}

/**
 * 打开新增弹窗
 *
 * @param Id ID
 */
async function handleOpenDialog(Id?:string,data?:LevelVO) {

  dialog.visible = true;
  if (Id) {
    dialog.title = "修改";
    Object.assign(formData, data);
  } else {
    dialog.title = "新增";
  }
}

// 提交表单
function handleSubmit() {
  FormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (formData.id) {
        ElMessage.success("修改成功");
        // GroupAPI.update(formData.id, formData)
        //   .then(() => {
        //     ElMessage.success("修改成功");
        //     handleCloseDialog();
        //     handleQuery();
        //   })
        //   .finally(() => (loading.value = false));
      } else {
        ElMessage.success("新增成功");
        // GroupAPI.create(formData)
        //   .then(() => {
        //     ElMessage.success("新增成功");
        //     handleCloseDialog();
        //     handleQuery();
        //   })
        //   .finally(() => (loading.value = false));
      }
      handleCloseDialog();
      handleQuery();
      loading.value = false
    }
  });
}

// 删除
function handleDelete(Id?: number) {
  const Ids = [Id || selectIds.value].join(",");

  if (!Ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      ElMessage.success("删除成功");
      // GroupAPI.deleteByIds(Ids)
      //   .then(() => {
      //     ElMessage.success("删除成功");
      //     handleResetQuery();
      //   })
      //   .finally(() => (loading.value = false));
      loading.value = false
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

// 重置表单
function resetForm() {
  FormRef.value.resetFields();
  FormRef.value.clearValidate();

  formData.id = undefined;
  formData.status = 1;
  formData.sort = 1;
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  resetForm();
}

onMounted(() => {
  handleQuery();
});
</script>
